﻿using AutoMapper;
using B.S.Packaged.Domain;
using B.S.Packaged.ErrorCode;
using B.S.Packaged.WebAPI.Read.Application.Command.MaterialPreparation;
using B.S.Packaged.WebAPI.Read.Dtos;
using MediatR;
using SqlSugar;

namespace B.S.Packaged.WebAPI.Read.Application.CommandHander.MaterialPreparation
{
    public class GetMaterialHander : IRequestHandler<GetMaterialCommand, APIResult<List<MaterialDto>>>
    {

        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public GetMaterialHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        /// <summary>
        /// 显示物料信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<List<MaterialDto>>> Handle(GetMaterialCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<MaterialDto>> result = new APIResult<List<MaterialDto>>();

            var entity = db.Queryable<Material>().ToList();

            result.code = APIResultCode.success;
            result.msg = APIResultCode.success.ToString();
            result.data = mapper.Map<List<MaterialDto>>(entity);

            return Task.FromResult(result);
        }
    }
}
